<style lang="less">
  @import '../styles/common.less';
  @import '../tables/components/table.less';
</style>

<template>
  <div>
    <Row :gutter="10">
      <Card>
        <div class="table-head">
          <Button type="primary" class="Button" @click="ExportData()">导出数据</Button>
          <Button type="primary" class="Button" @click="importData()">导入数据</Button>
          <a href="/vendors/Excel/hotline_service.xlsx" download="热线服务.xlsx">
            <Button type="primary" class="Button">下载模板</Button>
          </a>
          <Button type="primary" class="Button" @click="remove()">删除</Button>
          <Button type="primary" class="Button" @click="add()">添加</Button>
        </div>
        <Row class="margin-top-10">
          <Table stripe border :columns="columns" :data="data" @on-row-click="onRowData"
                 @on-selection-change="selectionChange"></Table>
        </Row>
        <div style="margin: 10px;overflow: hidden;height: 36px;">
          <div style="float: right;">
            <Page :total="total" :current="page" @on-change="changePage" show-elevator></Page>
          </div>
        </div>
      </Card>
      <Modal :width="600" title="详情"
             v-model="showCurrentTableData"
             ok-text="确定"
             cancel-text="取消"
             @on-ok=""
             @on-cancel=""
             class-name="vertical-center-modal"
             class="Modal-Detail-div">
        <Form label-position="top">
          <FormItem label="标题" v-if="dataDetail.title">
            <div class="dataDetail-div" v-text="dataDetail.title"></div>
          </FormItem>
          <FormItem label="姓名" v-if="dataDetail.name">
            <div class="dataDetail-div" v-text="dataDetail.name"></div>
          </FormItem>
          <FormItem label="电话" v-if="dataDetail.phone">
            <div class="dataDetail-div" v-text="dataDetail.phone"></div>
          </FormItem>
          <FormItem label="添加时间" v-if="dataDetail.create_time">
            <div class="dataDetail-div" v-text="dataDetail.create_time"></div>
          </FormItem>
          <FormItem label="地址" v-if="dataDetail.address">
            <div class="dataDetail-div" v-text="dataDetail.address"></div>
          </FormItem>
        </Form>
      </Modal>
      <!--编辑-->
      <Modal v-model="addCurrentTableData" :width="600" :mask-closable="false" :closable="false"
             class-name="vertical-center-modal" class="Modal-Detail-div">
        <h3 slot="header" style="color:#2D8CF0">编辑</h3>
        <Form label-position="top">
          <FormItem label="标题">
            <Input v-model="addData.title" placeholder="标题"></Input>
          </FormItem>
          <FormItem label="姓名">
            <Input v-model="addData.name" placeholder="姓名"></Input>
          </FormItem>
          <FormItem label="电话">
            <Input v-model="addData.phone" placeholder="电话"></Input>
          </FormItem>
          <FormItem label="地址">
            <Input v-model="addData.address" type="textarea" class="textarea_resize"
                   :autosize="{minRows: 3}" placeholder="地址"></Input>
          </FormItem>
        </Form>
        <div slot="footer">
          <Button type="text" @click="hideAddModal">取消</Button>
          <Button type="primary" :loading="saveLoading" @click="save()">确定</Button>
        </div>
      </Modal>
      <!--导入-->
      <Modal v-model="importDataModal" :width="600" :mask-closable="false" :closable="false"
             class-name="vertical-center-modal" class="Modal-Detail-div">
        <h3 slot="header" style="color:#2D8CF0">导入信息</h3>
        <Form label-position="top">
          <FormItem label="上传附件">
            <div class="dataDetail-div chooseUser accessory">
              <div class="users" v-for="el in importDataArr">
                <span class="span-name" v-text="el.name"></span>
                <span class="span-icon" @click="delimportData(el)">
                  <Icon type="close-round"></Icon>
                </span>
              </div>
              <Upload v-if="importDataArr.length == 0"
                      :before-upload="handleUpload" action="">
                <Button icon="ios-cloud-upload-outline">选择文件</Button>
              </Upload>
            </div>
          </FormItem>
          <FormItem label="预览" v-if="ImportData.length > 0">
            <div class="dataDetail-div dataDetail-div-padding-0">
              <Table stripe border :columns="ImportDataColumns" :data="ImportData" height="360"></Table>
            </div>
          </FormItem>
        </Form>
        <div slot="footer">
          <Button type="text" @click="hideImportDataModal">取消</Button>
          <Button type="primary" :loading="saveLoading" @click="saveImportData()">确定</Button>
        </div>
      </Modal>
    </Row>
  </div>
</template>

<script>
  import Cookies from 'js-cookie';
  import Util from '../../libs/util';
  import Utils from '../../libs/mod.utils';
  import String from '../../libs/mod.string';
  import Modal from '../../libs/mod.Modal';
  export default {
    components: {},
    data () {
      return {
        total: 0,
        page: 1,
        size: 10,
        id: '',
        searchConName: '',
        searchConTel: '',
        showCurrentTableData: false,
        loading: true,
        showCurrentColumns: [],
        editInlineAndCellData: [],
        columns: [
          {
            type: 'selection',
            width: 60,
            align: 'center'
          },
          {
            key: 'title',
            title: '标题',
          },
          {
            key: 'name',
            title: '姓名',
          },
          {
            key: 'phone',
            title: '电话',
          },
          {
            key: 'create_time',
            title: '时间',
            width: 200
          },
          {
            title: 'Action',
            key: 'action',
            width: 150,
            align: 'center',
            render: (h, params) => {
              return h('div', [
                h('Button', {
                  props: {
                    type: 'primary',
                    size: 'small'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: e => {
                      e.stopPropagation();
                      var data = params.row;
                      this.update(data, params.index);
                    }
                  }
                }, '修改')
              ]);
            }
          }
        ],
        data: [],
        dataDetail: {},
        initTable: [],
        addCurrentTableData: false,
        saveLoading: false,
        ajaxType: 'add',
        addData: {
          title: '',
          name: '',
          phone: '',
          address: '',
        },
        selectionArr: [],
        importDataModal: false,
        importDataArr: [],
        ImportDataColumns: [],
        ImportData: [],
      };
    },
    mounted () {
      this.getData();
    },
    methods: {
      getData () {
        var _this = this;
        var data = {
          uid: Util.toJson(localStorage.getItem('userInfo')).id,
          page: _this.page,
          size: _this.size,
        };
        var params = {
          url: '/hotline-service/list',
          type: Util.method.get,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
        function _successFun(res) {
          if (res.result == 0) {
            var _data = res.data || [];
            _this.total = res.rows;
            _this.data = _data;
          } else {
            _this.$Message.error('查询信息失败!');
          }
        }
      },
      handleSearch () {
        var _this = this;
        _this.page = 1;
        var searchConName = _this.searchConName;
      },
      changePage (page) {
        var _this = this;
        console.log('page', page);
        _this.page = page;
        _this.getData();
      },
      onRowData: function (data, index) {
        var _this = this;
        _this.show(index, data);
      },
      show: function (index, data) {
        var _this = this;
        var id = data.id;
        _this.id = id;

        var data = {
          id: _this.id,
        };
        var params = {
          url: '/hotline-service/query',
          type: Util.method.get,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
        function _successFun(res) {
          if (res.result == 0) {
            var _data = res.data || [];
            _this.dataDetail = _data;
            _this.showCurrentTableData = true;
          } else {
            _this.$Message.error('查询信息失败!');
          }
        }
      },
      asyncOK: function () {
        var _this = this;
        _this.showCurrentTableData = false;
      },
      // 列表多选 选中数据
      selectionChange: function (selection) {
        var _this = this;
        _this.selectionArr = selection;
      },
      // 隐藏编辑页面
      hideAddModal: function () {
        var _this = this;
        _this.addCurrentTableData = false;
      },
      // 显示添加界面
      add: function () {
        var _this = this;
        _this.ajaxType = 'add';
        _this.addData.title = '';
        _this.addData.name = '';
        _this.addData.phone = '';
        _this.addData.address = '';
        _this.addCurrentTableData = true;
      },
      // 保存数据
      save: function () {
        var _this = this;
        var title = _this.addData.title;
        var name = _this.addData.name;
        var phone = _this.addData.phone;
        var address = _this.addData.address;
        if (!(name && name != '')) {
          _this.$Message.error('请填写姓名!');
          return false;
        }
        if (!(phone && phone != '')) {
          _this.$Message.error('请填写电话!');
          return false;
        }
        var data = {
          id: _this.id,
          uid: Util.toJson(localStorage.getItem('userInfo')).id,
          title: title,
          name: name,
          phone: phone,
          address: address,
        };
        var url = '/hotline-service/add';
        if (_this.ajaxType == 'update') {
          url = '/hotline-service/update';
        }
        var params = {
          url: url,
          type: Util.method.get,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
        _this.saveLoading = true;
        function _successFun(res) {
          _this.saveLoading = false;
          if (res.result == 0) {
            _this.page = 1;
            _this.getData();
            _this.addCurrentTableData = false;
            _this.addData.title = '';
            _this.addData.name = '';
            _this.addData.phone = '';
            _this.addData.address = '';
          } else {
            _this.$Message.error('保存信息失败!');
          }
        }

      },
      // 显示修改界面
      update: function (data, index) {
        var _this = this;
        _this.ajaxType = 'update';
        var id = data.id;
        _this.id = id;
        var data = {
          id: _this.id,
        };
        var params = {
          url: '/hotline-service/query',
          type: Util.method.get,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
        function _successFun(res) {
          if (res.result == 0) {
            var _data = res.data || [];
            _this.addData.title = _data.title;
            _this.addData.name = _data.name;
            _this.addData.phone = _data.phone;
            _this.addData.address = _data.address;
            _this.addCurrentTableData = true;
          } else {
            _this.$Message.error('查询信息失败!');
          }
        }
      },
      // 删除
      remove: function () {
        var _this = this;
        var selectionArr = _this.selectionArr;
        if (selectionArr.length == 0) {
          _this.$Message.error('请填选择要删除的数据!');
          return false;
        }
        var selectionIds = [];
        for (var k in selectionArr) {
          if (!selectionArr[k]) {
            continue;
          }
          var id = selectionArr[k].id;
          selectionIds.push(id);
        }
        Modal.confirm(_this, {content: '确定删除选中的信息？'}, function () {
          var data = {
            id: selectionIds,
          };
          var params = {
            url: '/hotline-service/deleteMultiple',
            type: Util.method.get,
            data: data,
            success: _successFun
          };
          Util.ajax(_this, params);
          function _successFun(res) {
            if (res.result == 0) {
              _this.page = 1;
              _this.getData();
            } else {
              _this.$Message.error('删除信息失败!');
            }
          }
        }, function () {
        });
      },
      // 导出
      ExportData: function () {
        var _this = this;
        var fields = [
          {
            field: 'title',
            title: '标题',
            type: 'varchar',
            order: 1,
          },
          {
            field: 'name',
            title: '姓名',
            type: 'varchar',
            order: 2,
          },
          {
            field: 'phone',
            title: '电话',
            type: 'varchar',
            order: 3,
          },
          {
            field: 'address',
            title: '地址',
            type: 'varchar',
            order: 4,
          },
          {
            field: 'create_time',
            title: '创建时间',
            type: 'varchar',
            order: 5,
          },
        ];
        var fieldsStr = Util.toStr(fields);
        var data = {
          uid: Util.toJson(localStorage.getItem('userInfo')).id,
          type: 'hotlineService',
          fields: fieldsStr,
        };
        var params = {
          url: '/file/db2excle',
          type: Util.method.post,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
        function _successFun(res) {
          if (res.result == 0) {
            var url = res.data;
            var date = Utils.dateToDateString(new Date());
            Utils.DownloadFile(url, '导出热线服务_' + date + '.xlsx');
          } else {
            _this.$Message.error('导出信息失败!');
          }
        }
      },
      // 导入
      importData: function () {
        var _this = this;
        _this.importDataArr = [];
        _this.ImportDataColumns = [];
        _this.ImportData = [];
        _this.importDataModal = true;
      },
      // 取消导入
      hideImportDataModal: function () {
        var _this = this;
        _this.importDataArr = [];
        _this.ImportDataColumns = [];
        _this.ImportData = [];
        _this.importDataModal = false;
      },
      // 确定导入
      saveImportData: function () {
        var _this = this;
        if (_this.importDataArr.length == 0) {
          _this.$Message.error('请上传附件!');
          return false;
        }
        var file_id = _this.importDataArr[0].id;
        var data = {
          uid: Util.toJson(localStorage.getItem('userInfo')).id,
          fid: file_id
        };
        var params = {
          url: '/hotline-service/importExcel',
          type: Util.method.get,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
        function _successFun(res) {
          if (res.result == 0) {
            _this.page = 1;
            _this.getData();
            _this.hideImportDataModal();
          } else {
            _this.$Message.error('导入信息失败!');
          }
        }
      },
      // 选中文件
      handleUpload: function (file) {
        var _this = this;
        _this.UploadFile(file);
        return false;
      },
      // 上传文件
      UploadFile: function (file) {
        var _this = this;
        var name = file.name;
        var arr = name.split('.');
        var type = arr[1];
        if (type != 'xlsx') {
          _this.$Message.error('请上传xlsx文件!');
          return false;
        }
        var formData = new FormData();
        formData.append("file", file);
        formData.append("action", "UploadVMKImagePath");
        var params = {
          url: '/file/addFile',
          type: Util.method.post,
          data: formData,
          success: _successFun
        };
        Util.ajax(_this, params);
        function _successFun(res) {
          if (res.result == 0) {
            var data = res.data;
            var id = data.id;
            var name = data.name;
            var url = data.url;
            var obj = {
              id: id,
              name: name,
              url: url,
            };
            _this.importDataArr.push(obj);
            _this.getImportData(id);// 预览
          } else {
            _this.$Message.error('上传失败!');
          }
        }
      },
      // 删除附件
      delimportData: function (el) {
        var _this = this;
        var id = el.id;
        var importDataArr = _this.importDataArr;
        for (var k in importDataArr) {
          if (!importDataArr[k]) {
            continue;
          }
          if (id == importDataArr[k].id) {
            var del = importDataArr.splice(k, 1);
            break;
          }
        }
        _this.importDataArr = importDataArr;
        _this.ImportDataColumns = [];
        _this.ImportData = [];
      },
      // 预览数据
      getImportData: function (file_id) {
        var _this = this;
        if (!file_id) {
          return;
        }
        var data = {
          fid: file_id,
          lines: 10,
        };
        var params = {
          url: '/file/exclePreview',
          type: Util.method.get,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
        function _successFun(res) {
          if (res.result == 0) {
            var _data = res.data;
            var ImportDataColumns = [];
            var ImportData = [];
            if (_data.length > 0) {
              var Sheet1_data = _data[0].data;
              if (Sheet1_data.length > 0) {
                // 表头
                var titleArr = Sheet1_data[0];
                for (var t in titleArr) {
                  if (!titleArr[t]) {
                    continue;
                  }
                  var col = titleArr[t].col;
                  var title = titleArr[t].title;
                  var Columns = {
                    key: 'key_' + title + '_' + col,
                    title: title,
                    width: 150,
                  };
                  ImportDataColumns.push(Columns);
                }
                // 数据
                for (var k in Sheet1_data) {
                  if (!Sheet1_data[k]) {
                    continue;
                  }
                  var dataObj = {};
                  var dataArr = Sheet1_data[k];
                  for (var r in dataArr) {
                    if (!dataArr[r]) {
                      continue;
                    }
                    var col = dataArr[r].col;
                    var title = dataArr[r].title;
                    var value = dataArr[r].value;
                    var key = 'key_' + title + '_' + col;
                    dataObj[key] = value;
                  }
                  if (Object.keys(dataObj).length > 0) {
                    ImportData.push(dataObj);
                  }
                }
              }
            }
            _this.ImportDataColumns = ImportDataColumns;
            _this.ImportData = ImportData;
          } else {
            _this.$Message.error('预览信息失败!');
          }
        }
      },

    }
  };
</script>
